package krow.dev.gunpobus.util;

import krow.dev.gunpobus.Constants.Value;
import krow.dev.gunpobus.model.NodeItem.NodeType;
import krow.dev.gunpobus.R;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class ItemViewFactory {

	public static View createRouteView(LayoutInflater layoutInflater, String name, int type, Object tag){
		
		View view = layoutInflater.inflate(R.layout.item_route, null);
		view.setTag(tag);
		
		View iconView = view.findViewById(R.id.view_icon);
		iconView.setBackgroundResource(getRouteResourceId(type));
		
		TextView nameView = (TextView) view.findViewById(R.id.text_name);
		nameView.setTextColor(getRouteColor(type));
		nameView.setText(name);
		
		return view;
	}
	
	public static View createStationView(LayoutInflater layoutInflater, String name, String serial, Object tag){
		
		View view = layoutInflater.inflate(R.layout.item_station, null);
		view.setTag(tag);
		
		TextView nameView = (TextView) view.findViewById(R.id.text_name);
		nameView.setText(name);
		
		TextView serialView = (TextView) view.findViewById(R.id.text_serial);
		serialView.setText(serial);
		
		return view;
	}
	
	public static View createScheduleView(LayoutInflater layoutInflater, String name, int type, String licensePlate, String estimatedMinute, String estimatedStation, Object tag){
		
		View view = layoutInflater.inflate(R.layout.item_schedule, null);
		view.setTag(tag);
		
		TextView nameView = (TextView) view.findViewById(R.id.text_name);
		nameView.setTextColor(getRouteColor(type));
		nameView.setText(name);
		
		TextView licensePlateView = (TextView) view.findViewById(R.id.text_license_plate);
		licensePlateView.setText(licensePlate);
		
		TextView minuteView = (TextView) view.findViewById(R.id.text_estimated_minute);
		TextView stationView = (TextView) view.findViewById(R.id.text_estimated_station);
		if(estimatedStation != null && estimatedStation.trim().length() > 0){
			minuteView.setText(estimatedMinute);
			stationView.setText(estimatedStation);
		}else{
			minuteView.setText(layoutInflater.getContext().getString(R.string.text_bus_not_found));
		}
		
		return view;
	}
	
	public static View createNodeView(LayoutInflater layoutInflater, String name, boolean hasBus, int routeType, NodeType nodeType, Object tag){
		
		View view = layoutInflater.inflate(R.layout.item_node, null);
		view.setTag(tag);
		
		TextView nameView = (TextView) view.findViewById(R.id.text_name);
		nameView.setText(name);

		if(hasBus){
			View iconView = view.findViewById(R.id.view_icon);
			iconView.setBackgroundResource(getRouteResourceId(routeType));
		}
		
		View topLineView = view.findViewById(R.id.view_top_line);
		View bottomLineView = view.findViewById(R.id.view_bottom_line);
		View pointView = view.findViewById(R.id.view_point);

		int firstHalfColor = 0xFF489AD8;
		int secondHalfColor = 0xFFD95F49;
		switch (nodeType) {
		case First_Half:
			topLineView.setBackgroundColor(firstHalfColor);
			bottomLineView.setBackgroundColor(firstHalfColor);
			break;
		case Second_Half:
			topLineView.setBackgroundColor(secondHalfColor);
			bottomLineView.setBackgroundColor(secondHalfColor);
			break;
		case Start:
			bottomLineView.setBackgroundColor(firstHalfColor);
			pointView.setBackgroundResource(R.drawable.shape_point_start);
			break;
		case End:
			topLineView.setBackgroundColor(secondHalfColor);
			pointView.setBackgroundResource(R.drawable.shape_point_end);
			break;
		case End_No_Return:
			topLineView.setBackgroundColor(firstHalfColor);
			pointView.setBackgroundResource(R.drawable.shape_point_start);
			break;
		case Retrun:
			topLineView.setBackgroundColor(firstHalfColor);
			bottomLineView.setBackgroundColor(secondHalfColor);
			break;
		}
		
		return view;
	}
	
	private static int getRouteColor(int type) {
		
		int color = 0;
		switch (type) {
		case Value.TYPE_ROUTE_SEAT:
			color = 0xFF489AD8;
			break;
		case Value.TYPE_ROUTE_VILLAGE:
			color = 0xFFEDC233;
			break;
		case Value.TYPE_ROUTE_DIRECT:
			color = 0xFfD95F49;
			break;
		case Value.TYPE_ROUTE_GENERAL:
			color = 0xFF64C271;
			break;
		}
		return color;
	}
	
	private static int getRouteResourceId(int type) {
		
		int resourceId = R.drawable.icon_bus_blue;
		
		switch (type) {
		case Value.TYPE_ROUTE_SEAT:
			resourceId = R.drawable.icon_bus_blue;
			break;
		case Value.TYPE_ROUTE_VILLAGE:
			resourceId = R.drawable.icon_bus_yellow;
			break;
		case Value.TYPE_ROUTE_DIRECT:
			resourceId = R.drawable.icon_bus_red;
			break;
		case Value.TYPE_ROUTE_GENERAL:
			resourceId = R.drawable.icon_bus_green;
			break;
		}
		
		return resourceId;
	}
}
